package com.jay.nio.channel.clazz;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.SocketChannel;
import java.nio.channels.WritableByteChannel;

public class ChannelsCls {
	public static void main(String[] args) throws IOException,
			ClassNotFoundException {

		byte[] inData = { 96, 97, 98, 99, 100 };
		ByteArrayInputStream bin = new ByteArrayInputStream(inData);
		ReadableByteChannel rbc = Channels.newChannel(bin);
		ByteBuffer buf = ByteBuffer.allocate(256);
		rbc.read(buf);
		if (rbc.isOpen()) {
			rbc.close();
		}
		bin.close();

		buf.flip();
		byte[] content = new byte[buf.limit()];
		buf.get(content);
		System.out.println(new String(content));
		System.out.println("---------------");

		ByteArrayOutputStream bout = new ByteArrayOutputStream();
		WritableByteChannel wbc = Channels.newChannel(bout);

		buf = ByteBuffer.allocate(256);
		buf.put((byte) 97);
		buf.put((byte) 98);
		buf.put((byte) 99);
		buf.put((byte) 100);
		buf.put((byte) 101);
		buf.put((byte) 102);
		buf.put((byte) 103);
		buf.flip();
		wbc.write(buf);
		wbc.close();
		bout.close();

		System.out.println(new String(bout.toByteArray()));
		System.out.println("---------------");

		File f = new File("out.txt");
		FileOutputStream fos = new FileOutputStream(f);
		wbc = fos.getChannel();
		OutputStream out = Channels.newOutputStream(wbc);
		out.write(48);
		out.write(48);
		out.write(48);
		out.write(48);
		wbc.close();
		fos.close();
		System.out.println("---------------");

		FileInputStream fis = new FileInputStream(f);
		rbc = fis.getChannel();
		InputStream in = Channels.newInputStream(rbc);
		int b;
		while ((b = in.read()) != -1) {
			System.out.println(b);
		}
		rbc.close();
		fis.close();
		System.out.println("---------------");

		f = new File("code.txt");
		fos = new FileOutputStream(f);
		wbc = fos.getChannel();
		Writer writer = Channels.newWriter(wbc, "GB2312");

		String text = "xx中国";
		writer.write(text);

		writer.flush();
		writer.close();
		fos.close();
		wbc.close();

		System.out.println("---------------");

		f = new File("code.txt");
		fis = new FileInputStream(f);
		rbc = fis.getChannel();
		Reader reader = Channels.newReader(rbc, "GB2312");

		char[] array = new char[1];
		text = "";
		while (reader.read(array) != -1) {
			text += array[0];
		}
		System.out.println(text);
		fis.close();
		rbc.close();
		reader.close();
		
		NonBlocking();
	}

	static private void NonBlocking() throws IOException,
			ClassNotFoundException {
		SocketChannel channel = SocketChannel.open();
		channel.connect(new InetSocketAddress("localhost", 22));
		channel.configureBlocking(false);
		InputStream in = Channels.newInputStream(channel);
		ObjectInputStream objIn = new ObjectInputStream(in);

		// The next line throws an IllegalBlockingModeException
		Object object = objIn.readObject();

		OutputStream out = Channels.newOutputStream(channel);
		ObjectOutputStream objOut = new ObjectOutputStream(out);

		// The next line throws an IllegalBlockingModeException
		objOut.writeObject(object);
	}
}
